package com.gec.anan.order.handle;


import com.gec.anan.common.execption.AnanException;
import com.gec.anan.common.result.ResultCodeEnum;
import com.gec.anan.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

//监听延迟队列
@Component
public class RedisDelayHandle {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private OrderInfoService orderInfoService;

    @PostConstruct
    public void listener(){
        new Thread(()->{
            while (true){
                //从延迟队列中获取订单号
                RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque("queue-cancel");
                try {
                    //转化并获取订单号
                    String orderId = blockingDeque.take();
                    if(StringUtils.hasText(orderId)){
                        orderInfoService.orderCancel(Long.parseLong(orderId));
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    throw new AnanException(ResultCodeEnum.DATA_ERROR );
                }
            }
        }).start();
    }
}
